package com.leonzx.base.controller.email;

import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.email.MailHistory;
import com.leonzx.base.service.mail.MailHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:chen
 * @Description:
 * @Date: 2018-07-06 15:09
 */
@Controller
@RequestMapping("/email/")
public class EmailController extends BaseController {

    @Autowired
    private MailHistoryService mailHistoryService;

    /**
     *跳转邮件编辑页面
    *@Author chen
    *@Date 2018/7/6 15:11
    */
    @RequestMapping("/edit")
    public String addMail() {
        return "email/edit";
    }

    @RequestMapping("/send")
    public String sendEmail(MailHistory mailHistory, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
        try {
            mailHistoryService.sendEmail(mailHistory, currentUser().getId(), request, response);
            addSuccessMessage(redirectAttributes, "发送成功");
        } catch (Exception e) {
            logger.error("发送失败：{}", e);
            addFailMessage(redirectAttributes, "发送失败");
        }
        return "redirect:/email/edit";
    }

    @RequestMapping("/emailList")
    public String emailList(){
        return "email/list";
    }

    /**
     * 邮件详情
    *@Author chen
    *@Date 2018/7/17 9:13
    */
    @RequestMapping("/emailDetail")
    public String emailDetail(Model model, String id){
        MailHistory mailHistory = mailHistoryService.findMailDetailByHistoryId(id);
        model.addAttribute("mailHistory", mailHistory);
        return "email/detail";
    }
}
